# Run analyses ------------------------------------------------------------


# lc1
lc1_el <- ols_main(
  outcome = "lc1",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$lc1,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

lc1_el_pvals <- get_RI_pvals(
  outcome = "lc1",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$lc1,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


lc1_ml <- ols_main(
  outcome = "lc1_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$lc1_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

lc1_ml_pvals <- get_RI_pvals(
  outcome = "lc1_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$lc1_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# pta

pta_el <- ols_main(
  outcome = "pta",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$pta,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

pta_el_pvals <- get_RI_pvals(
  outcome = "pta",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$pta,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


pta_ml <- ols_main(
  outcome = "pta_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$pta_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

pta_ml_pvals <- get_RI_pvals(
  outcome = "pta_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$pta_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# bring_up

bring_up_el <- ols_main(
  outcome = "bring_up",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$bring_up,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

bring_up_el_pvals <- get_RI_pvals(
  outcome = "bring_up",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$bring_up,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


bring_up_ml <- ols_main(
  outcome = "bring_up_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$bring_up_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

bring_up_ml_pvals <- get_RI_pvals(
  outcome = "bring_up_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$bring_up_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# assemble


assemble_el <- ols_main(
  outcome = "assemble",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$assemble,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

assemble_el_pvals <- get_RI_pvals(
  outcome = "assemble",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$assemble,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


assemble_ml <- ols_main(
  outcome = "assemble_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$assemble_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

assemble_ml_pvals <- get_RI_pvals(
  outcome = "assemble_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$assemble_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")

# absenteeism_action (ml, el and total)
absenteeism_action_el <- ols_main(
  outcome = "absenteeism_action",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$absenteeism_action,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

absenteeism_action_el_pvals <- get_RI_pvals(
  outcome = "absenteeism_action",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$absenteeism_action,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")

absenteeism_action_ml <- ols_main(
  outcome = "absenteeism_action_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$absenteeism_action_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

absenteeism_action_ml_pvals <- get_RI_pvals(
  outcome = "absenteeism_action_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$absenteeism_action_ml,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")

absenteeism_action_total <- ols_main(
  outcome = "absenteeism_action_total",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$absenteeism_action_total,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

absenteeism_action_total_pvals <- get_RI_pvals(
  outcome = "absenteeism_action_total",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = ABS_lasso_covariates_panel$absenteeism_action_total,
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")



# Make table --------------------------------------------------------------

#setup
control_means <- with(
  el, 
  c(
    "Control Mean",
    round(mean(lc1_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(lc1[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(bring_up_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(bring_up[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(pta_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(pta[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(assemble_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(assemble[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(absenteeism_action_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(absenteeism_action[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
    round(mean(absenteeism_action_total[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2)
  )
)


pval_lines <- c(
  "RI $p$-values",
  round(lc1_ml_pvals$ri_pvals["absenteeism"],3),
  round(lc1_el_pvals$ri_pvals["absenteeism"],3),
  round(bring_up_ml_pvals$ri_pvals["absenteeism"],3),
  round(bring_up_el_pvals$ri_pvals["absenteeism"],3),
  round(pta_ml_pvals$ri_pvals["absenteeism"],3),
  round(pta_el_pvals$ri_pvals["absenteeism"],3),
  round(assemble_ml_pvals$ri_pvals["absenteeism"],3),
  round(assemble_el_pvals$ri_pvals["absenteeism"],3),
  round(absenteeism_action_ml_pvals$ri_pvals["absenteeism"],3), #note 3-digit p values 
  round(absenteeism_action_el_pvals$ri_pvals["absenteeism"],3),
  round(absenteeism_action_total_pvals$ri_pvals["absenteeism"],3)
)

hypothesis_lines <- c(
  "Hypothesis",
  "upr",
  "upr",
  "upr",
  "upr",
  "upr",
  "upr",
  "upr",
  "upr",
  "upr",
  "upr",
  "upr"
)

covariate_counts <- c(
  "LASSO covariates",
  length(ABS_lasso_covariates_panel$lc1_ml),
  length(ABS_lasso_covariates_panel$lc1),
  length(ABS_lasso_covariates_panel$bring_up_ml),
  length(ABS_lasso_covariates_panel$bring_up),
  length(ABS_lasso_covariates_panel$pta_ml),
  length(ABS_lasso_covariates_panel$pta), 
  length(ABS_lasso_covariates_panel$assemble_ml),
  length(ABS_lasso_covariates_panel$assemble), 
  length(ABS_lasso_covariates_panel$absenteeism_action_ml),
  length(ABS_lasso_covariates_panel$absenteeism_action),
  length(ABS_lasso_covariates_panel$absenteeism_action_total)
)



#make tables
sink("03_tables/ABS_conative_attitudes_lasso.tex")
stargazer(
  ... = list(
    lc1_ml$fit,
    lc1_el$fit,
    bring_up_ml$fit,
    bring_up_el$fit,
    pta_ml$fit,
    pta_el$fit,
    assemble_ml$fit,
    assemble_el$fit,
    absenteeism_action_ml$fit,
    absenteeism_action_el$fit,
    absenteeism_action_total$fit
  ),
  type = "latex",
  p = list(
    lc1_ml_pvals$ri_pvals,
    lc1_el_pvals$ri_pvals,
    bring_up_ml_pvals$ri_pvals,
    bring_up_el_pvals$ri_pvals,
    pta_ml_pvals$ri_pvals,
    pta_el_pvals$ri_pvals,
    assemble_ml_pvals$ri_pvals,
    assemble_el_pvals$ri_pvals,
    absenteeism_action_ml_pvals$ri_pvals,
    absenteeism_action_el_pvals$ri_pvals,
    absenteeism_action_total_pvals$ri_pvals
  ),
  se = list(
    lc1_ml$fit_summary[,"Std. Error"],
    lc1_el$fit_summary[,"Std. Error"],
    bring_up_ml$fit_summary[,"Std. Error"],
    bring_up_el$fit_summary[,"Std. Error"],
    pta_ml$fit_summary[,"Std. Error"],
    pta_el$fit_summary[,"Std. Error"],
    assemble_ml$fit_summary[,"Std. Error"],
    assemble_el$fit_summary[,"Std. Error"],
    absenteeism_action_ml$fit_summary[,"Std. Error"],
    absenteeism_action_el$fit_summary[,"Std. Error"],
    absenteeism_action_total$fit_summary[,"Std. Error"]
  ),
  keep = "absenteeism",
  omit.stat = c("rsq","f","ser"),
  column.separate = c(2,2,2,2,3),
  column.labels = c("Involve LC1 Chair","Tell village","Use PTA","Assemble group","Index"),
  table.layout = "=cd#-t-as=n",
  dep.var.labels = c("Midline","Endline",
                     "Midline","Endline",
                     "Midline","Endline",
                     "Midline","Endline",
                     "Midline","Endline","Overall" #note that "Overall" is the absenteeism_action_total
  ),
  dep.var.labels.include = TRUE,
  no.space = T,
  omit = "block_id",
  add.lines = list(
    control_means,
    pval_lines,
    hypothesis_lines,
    c("Block FE",
      "Yes","Yes","Yes",
      "Yes","Yes","Yes", 
      "Yes", "Yes","Yes",
      "Yes","Yes"),
    covariate_counts),
  notes.label = "",
  float = FALSE
  # style = "qje"
)
sink()



